package com.tourism.www;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.t2cn.common.www.ServletCommon;
import com.tourism.storage.dao.PartnerDao;
import com.tourism.storage.pojo.Partner;
import com.tourism.www.common.TourismBaseHandler;

public class PartnerHandler extends TourismBaseHandler {

	@Override
	public String doActionX(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = getMethodName(request);
		if(method.equals("add")){
			return addPartner(request, response);
		}else if(method.equals("list")){
			return list(request, response);
		}else if(method.equals("page_add")){
			return "page_add";
		}else if(method.equals("delete")){
			return deletePartner(request, response);
		}else if(method.equals("getPartnerById")){
			return getPartnerById(request, response);
		}else if(method.equals("update")){
			return updatePartner(request, response);
		} 
		return null;
	}
	private String addPartner(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String partnername = ServletCommon.getParamValue(request, "partnername");
		System.out.println("partnername="+partnername);
		String phone = ServletCommon.getParamValue(request, "phone");
		String address = ServletCommon.getParamValue(request, "address");
		Partner pojo = new Partner();
		pojo.setAddress(address);
		pojo.setPartnername(partnername);
		pojo.setPhone(phone);
		pojo.setStatus(0);
		PartnerDao dao = new PartnerDao();
		dao.addPartner(pojo);
		
		return "list";
	}
	private String updatePartner(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer partnerid = ServletCommon.getIntParamValue(request, "partnerid", 0);
		String partnername = ServletCommon.getParamValue(request, "partnername");
		System.out.println("partnername="+partnername);
		String phone = ServletCommon.getParamValue(request, "phone");
		String address = ServletCommon.getParamValue(request, "address");
		Partner pojo = new Partner();
		pojo.setPartnerid(partnerid);
		pojo.setAddress(address);
		pojo.setPartnername(partnername);
		pojo.setPhone(phone);
		PartnerDao dao = new PartnerDao();
		dao.updatePartner(pojo);
		
		return "list";
	}
	private String list(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		PartnerDao dao = new PartnerDao();
		List<Partner> list = dao.getPartbers(0);
		request.setAttribute("partners", list);
		return "page_list";
	}
	private String getPartnerById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer partnerid = ServletCommon.getIntParamValue(request, "partnerid", 0);
		System.out.println("partnerid="+partnerid);
		PartnerDao dao = new PartnerDao();
		Partner pojo = dao.getPartberById(partnerid);
		request.setAttribute("pojo", pojo);
		return "page_update";
	}
	private String deletePartner(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer partnerid = ServletCommon.getIntParamValue(request, "partnerid", 0);
		System.out.println("partnerid="+partnerid);
		//String address = ServletCommon.getParamValue(request, "address");
		Partner pojo = new Partner();
		pojo.setPartnerid(partnerid);
		pojo.setStatus(1);
		PartnerDao dao = new PartnerDao();
		dao.updatePartner(pojo);
		
		return "list";
	}
}
